<?php
/**
 * Created by PhpStorm.
 * User: zhj
 * Date: 2019/4/12
 * Time: 11:04
 */

namespace app\common\logic;

use think\Db;

/**
 * 文件逻辑
 */
class Userfile extends LogicBase
{

    /**
     * 文件分类编辑
     */
    public function userfileCategoryEdit($data = [])
    {

        $validate_result = $this->validateUserfileCategory->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateUserfileCategory->getError()];
        }

        $url = url('userfileCategoryList');

        $result = $this->modelUserfileCategory->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '文件分类' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '操作成功', $url] : [RESULT_ERROR, $this->modelUserfileCategory->getError()];
    }

    /**
     * 获取文件列表
     */
    public function getUserfileList($where = [], $field = 'a.*,m.nickname,c.name as category_name,d.path', $order = '')
    {

        $this->modelUserfile->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
            [SYS_DB_PREFIX . 'userfile_category c', 'a.category_id = c.id'],
            [SYS_DB_PREFIX . 'file d', 'a.userfile_id = d.id'],
        ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelUserfile->join = $join;

        return $this->modelUserfile->getList($where, $field, $order);
    }

    /**
     * 获取文件列表搜索条件
     */
    public function getWhere($data = [])
    {

        $where = [];

        if(!empty($data['field'])){
            !empty($data['search_data']) && $where[$data['field']] = ['like', '%'.$data['search_data'].'%'];
        }

        return $where;
    }

    /**
     * 文件信息编辑
     */
    public function userfileEdit($data = [])
    {

        $validate_result = $this->validateUserfile->scene('edit')->check($data);

        if (!$validate_result) {

            return [RESULT_ERROR, $this->validateUserfile->getError()];
        }

        $url = url('userfileList');

        empty($data['id']) && $data['member_id'] = MEMBER_ID;

        //$data['content'] = html_entity_decode($data['content']);

        $result = $this->modelUserfile->setInfo($data);

        $handle_text = empty($data['id']) ? '新增' : '编辑';

        $result && action_log($handle_text, '文件' . $handle_text . '，name：' . $data['name']);

        return $result ? [RESULT_SUCCESS, '文件操作成功', $url] : [RESULT_ERROR, $this->modelUserfile->getError()];
    }

    /**
     * 获取文件信息
     */
    public function getUserfileInfo($where = [], $field = 'a.*,m.nickname,c.name as category_name')
    {

        $this->modelUserfile->alias('a');

        $join = [
            [SYS_DB_PREFIX . 'member m', 'a.member_id = m.id'],
            [SYS_DB_PREFIX . 'userfile_category c', 'a.category_id = c.id'],
        ];

        $where['a.' . DATA_STATUS_NAME] = ['neq', DATA_DELETE];

        $this->modelUserfile->join = $join;
        $info =  $this->modelUserfile->getInfo($where, $field);
        if(!empty($info['province'])){
            $info['province'] = $this->logicRegion->getRegionList(['name' => $info['province'],'upid'=>0, 'level' => 1])->toArray()[0]['id'];
        }

        if(!empty($info['city'])){
            $city =  $this->logicRegion->getRegionList(['name' => $info['city'],'upid'=>$info['province'] ,'level' => 2])->toArray();
            $info['city'] = $city?$city[0]['id']:0;
        }
        if(!empty($info['county'])){
            $conty =$this->logicRegion->getRegionList(['name' => $info['county'],'upid'=>$info['city'], 'level' => 3])->toArray() ;
            $info['county'] = $conty?$conty[0]['id']:0;
        }

        return $info;
    }

    /**
     * 获取分类信息
     */
    public function getUserfileCategoryInfo($where = [], $field = true)
    {

        return $this->modelUserfileCategory->getInfo($where, $field);
    }

    /**
     * 获取文件分类列表
     */
    public function getUserfileCategoryList($where = [], $field = true, $order = '', $paginate = 0)
    {

        return $this->modelUserfileCategory->getList($where, $field, $order, $paginate);
    }

    /**
     * 文件分类删除
     */
    public function userfileCategoryDel($where = [])
    {

        $result = $this->modelUserfileCategory->deleteInfo($where);

        $result && action_log('删除', '文件分类删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '文件分类删除成功'] : [RESULT_ERROR, $this->modelUserfileCategory->getError()];
    }

    /**
     * 文件删除
     */
    public function userfileDel($where = [])
    {

        $result = $this->modelUserfile->deleteInfo($where);

        $result && action_log('删除', '文件删除，where：' . http_build_query($where));

        return $result ? [RESULT_SUCCESS, '文件删除成功'] : [RESULT_ERROR, $this->modelUserfile->getError()];
    }
}
